﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thor.Common;
using Thor.Configuration;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Domain
{
    public class CalendarHelper
    {
        #region fields

        private static ConfigurationSettings _ConfigSettings = new ConfigurationSettings();
        private static string _ApplicationNameKey = ConfigurationSettings.ApplicationNameKey;
        private static string _ApplicationNameValue = ConfigurationSettings.ApplicationNameValue;
        private static string _ConnectionStringKey = ConfigurationSettings.ConnectionStringKey;
        private static string _ConnectionStringValue = ConfigurationSettings.ConnectionStringValue;
        private static string _RepositoryAssembly = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.CalendarRepositoryAssembly;
        private static string _RepositoryType = ConfigurationSettings.CalendarRepositoryType;

        #endregion

        #region constructors

        private CalendarHelper() { }

        #endregion

        #region properties

        private static ConfigurationSettings ConfigurationSettings
        {
            get
            {
                if (_ConfigSettings == null)
                    _ConfigSettings = new ConfigurationSettings();

                return _ConfigSettings;
            }
        }

        #endregion

        #region methods

        public static bool AreCalendarsConfigured()
        {
            var manager = GetManager();

            if (manager.Calendars != null && manager.Calendars.Count > 0)
                return true;
            else
                return false;
        }

        public static List<Calendar> GetCalendars()
        {
            return GetManager().Calendars;
        }

        public static Calendar GetCalendar(Guid calendarId)
        {
            return GetManager().GetCalendar(calendarId);
        }

        public static List<string> GetDistinctCalendarTimeZones()
        {
            return (from cal in GetCalendars()
                     select cal.TimeZone).Distinct().ToList();
        }

        public static List<Calendar> GetCalendarsByTimeZone(string timeZone)
        {
            return (from cal in GetCalendars()
                    where cal.TimeZone == timeZone
                    select cal).ToList();
        }

        public static void SaveCalendar(Calendar calendar)
        {
            GetManager().Update(calendar);
        }

        private static Dictionary<string, object> GetRepositoryParameters()
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add(_ApplicationNameKey, _ApplicationNameValue);
            parameters.Add(_ConnectionStringKey, _ConnectionStringValue);
            return parameters;
        }

        private static ICalendarRepository GetRepository(Dictionary<string, object> parameters)
        {
            return CalendarManager.GetRepository(_RepositoryAssembly, _RepositoryType, parameters);
        }

        public static CalendarManager GetManager()
        {
            Dictionary<string, object> parameters = GetRepositoryParameters();
            ICalendarRepository repository = GetRepository(parameters);
            CalendarManager manager = new CalendarManager(repository);
            return manager;
        }

        #endregion
    }
}
